﻿using System;
using System.IO;
using System.Collections.Generic;

namespace Game.Lib.CSG
{
    public abstract class Shape2D : Shape
    {
        public virtual Vector2 Pos { get; set; }
        public virtual  Vector2 Size { get; set; }
        public virtual float Rot { get; set; }
        public abstract float Area { get; }
        public abstract float Perimeter { get; }
        public abstract Rectangle GetBoundingRectangle();
        public abstract Ellipse GetBoundingCircle();
        public abstract bool Inside(Vector2 pos);

        public Intersection RayCast(Ray r, Plane p)
        {
            Intersection i = p.RayCast(r);
            if (i.Miss) return Intersection.Missed;
            Vector3 pos = i.Pos / p.Rot;
            if (Inside(new Vector2(pos.X, pos.Y))) return new Intersection(r, pos, p.Normal);
            return Intersection.Missed;
        }

        public override byte[] ToBytes()
        {
            MemoryStream ms = new MemoryStream();
            Deserialize(ms);
            return ms.ToArray();
        }

        public override void FromBytes(byte[] data, int pos)
        {
            MemoryStream ms = new MemoryStream(data);
            ms.Position = pos;
            Deserialize(ms);
        }

        public override abstract void Serialize(Stream s);
        public override abstract void Deserialize(Stream s);

        public abstract override List<Vector> CriticalPoints { get; }
    }
}
